<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/include/taglib.jsp"%>
<html>
<head>
<meta charset="UTF-8">
<title>我要投资-新手专享详情|购买</title>
<jsp:include page="/include/wchat/head.jsp"></jsp:include>

<link type="text/css" rel="stylesheet" href="/v2/wchat/resrc/css/invest.css">
</head>
<body>
<div id="header"> <a href="/wchatXsLoansDetail.do?id=${borrowDetailMap.id}" class="backto">返回</a>
  <div class="title-sub">新手专享详情</div>
</div>
<div id="wrap">
  <div class="invest-detail"><b class="icon-qiang"></b>
    <h1>
      
      
            稳盈宝 ${borrowDetailMap.borrowNumber }
      
    </h1>
    <ul class="items">
      <li class="c1">可购金额</li>
      <li class="c2"><s:property value="#request.borrowDetailMap.investAmount" default="0"/></li>
    </ul>
    <!--  
    <ul class="items">
      <li class="c1">红包</li>
      <li class="c2">￥<s:if test="%{#request.accmountStatisMap.tyjStatus == 0}">
							${accmountStatisMap.tyjAmount}
							<input type="hidden" class="input" value="${accmountStatisMap.tyjAmount}" id="investInput" >
						</s:if>
						<s:else>
							0.00
							<input type="hidden" class="input" value="0" id="investInput" >
						</s:else>
      <input type="hidden" id="realnameFlag" value="${session.user.isBindIdNo}">
      </li>
    </ul>
    -->
    <ul class="items">
      <li class="c1">安全保障</li>
      <li class="c2 safe">100%本息保障</li>
    </ul>
  </div>
  <div class="invest-buy">
    <ul>
      <li class="money">
        <label class="txt">购买金额</label>
        <s:if test="%{#request.accmountStatisMap.tyjStatus == 0}">
			<input type="hidden" class="input" value="${accmountStatisMap.tyjAmount}" id="investInput" >
	        <input class="input" id="inputAmount" maxlength="8" placeholder="红包￥${accmountStatisMap.tyjAmount}" readonly="readonly">
		</s:if>
		<s:else>
			<input type="hidden" class="input" value="0" id="investInput" >
			<input class="input" id="inputAmount" maxlength="8" placeholder="红包￥0.00" readonly="readonly">
		</s:else>
		<input type="hidden" id="realnameFlag" value="${session.user.isBindIdNo}">
      </li>     
      <li class="verify"> <span class="verify-code">
        <input type="text" id="authCode" class="input" maxlength="4" placeholder="请输入验证码">
        </span><img src="I4008lOve893gOfUll983/imageCode.do?pageId=xsWchatInvest" onclick="coderefresh()" id="verifyCodeImg"> </li>
      <li class="argee">
        <input type="checkbox" id="agreeContract" class="ipt-chk" checked="checked">
        我同意《<a href="/getMessageBytypeId.do?typeId=22">共富网投资咨询与管理服务协议</a>》</li>
    </ul>
    <div class="vtip" id="errorTip"></div>
  </div>
  <div class="operate-button operate-buy"> 
  	<a href="javascript:invest();" class="btn" id="investBt">我要购买</a> 
  	<a href="/wchatRealNameAuthen.do" class="btn" id="realNameBt" style="display: none;">立即实名认证</a>
  </div>
</div>
<jsp:include page="/include/wchat/footer.jsp"></jsp:include>
<script type="text/javascript">
var loan = {
		aunualInterestRate : '${borrowDetailMap.annualRate}',
		termCount : '${borrowDetailMap.deadline}',
		loanId : '${param.id}',
		descriptionLength : '${fn:length(borrowDetailMap.moneyPurposes)}',
		leftAmount : '${borrowDetailMap.investSum}',
		clientType : '${borrowUserMap.clientType}'
	};
environment.userCash = '${accmountStatisMap.usableSum}';
$(function(){
	/**
	var loanStatus = '300';
	if (loanStatus != "300") {
     window.location.href="/yuexitong/loanDetail.do?loanId=45185";
	}
	**/
  $('#agreeContract').click(function(){
    if ($('#agreeContract').attr("checked") == "checked") {
      $('#agreeContract').removeAttr("checked");
    } else {
      $('#agreeContract').attr("checked", "checked");
    }
  });
});
//刷新验证码
function coderefresh() {
    var verifyCode = $("#verifyCodeImg");
    var now = new Date();
    verifyCode.attr("src",'I4008lOve893gOfUll983/imageCode.do?pageId=xsWchatInvest&d=' + now.getTime());
}
//投资检查
function investCheck() {
	var errorTip = $('#errorTip');
    var leftAmount = Number(loan.leftAmount);// 可投金额
    var investInput = Number($('#investInput').val());
	var investAmountInput = $('.input-sum');
    var availableCouponAmount = Number($("#availableCouponAmount").html());
    var reg = /^\d+$/;
    if(leftAmount == 0){
    	errorTip.html('当前新手专享项目已满');
        return false;
    }
    if(reg.test(investInput)){
        if (investInput <= 0) {
        	errorTip.html('您的红包为0元，不满足本项投资');
			return false;
		}
        if((investInput-leftAmount)>0){
        	errorTip.html('当前最大可投'+leftAmount+'元');
            return false;
        }
        if(((leftAmount-investInput)<50)&&(leftAmount-investInput)!=0){
            var requiredLeftAmount = Number(leftAmount-50);
            if(requiredLeftAmount > 50){
            	errorTip.html('可投金额为'+leftAmount+'或50~'+requiredLeftAmount);
            }else if(requiredLeftAmount <= 50){
            	errorTip.html('可投金额为'+leftAmount);
            }
            return false;
        }
        var verifyCodeInput = $("#authCode");
        var verifyCode = verifyCodeInput.val();
        if(verifyCode == ''){
        	errorTip.html('请输入验证码');
            return false;
        }

        if($('#agreeContract').attr("checked") != "checked"){
        	errorTip.html('请同意共富网投资管理协议');
            return false;
        }
        //未实名认证，不能投资
    	if($("#realnameFlag").val() == "0"){
    		errorTip.html('未实名认证，不能投资');
    		$('#realNameBt').show();
			$('#investBt').hide();
            return false;
        }
        return true;
    }else{
    	errorTip.html('您的红包为0元，不满足本项投资');
        return false;
    }
}
function invest() {
  if(!investCheck()){
    return;
  };
  var errorTip = $('#errorTip');
  var investAmount = Number($('#investInput').val());
  var verifyCode = $("#authCode").val();
  
  var param = {};
  param['paramMap.id'] = loan.loanId;
  param["paramMap.pageId"] = "xsWchatInvest";
  param["paramMap.code"] = verifyCode;
  param['paramMap.amount'] = investAmount;
  
  $.ajax({
          dataType: 'json',
          url: environment.basePath+"/xsFinanceInvest.do",
          data: param,
          type: 'POST',
          beforeSend: function() {
              $("#investBt").attr("href","javascript:;").html("正在加入...").addClass("gbtn-disabled");
          },
          success: function(data) {
              if(data.msg == 1){
            	  alert("恭喜您，投资成功！");
                  window.location.href="/wchatXsLoansDetail.do?id="+loan.loanId;
              }else{
                  if(data.msg == '验证码错误'){
                      coderefresh();
                	  errorTip.html("验证码错误");
                      $("#investBt").attr("href","javascript:invest();").html("我要购买").removeClass("gbtn-disabled");
                  }else{
                	  coderefresh();
                	  errorTip.html(data.msg);
                      $("#investBt").attr("href","javascript:invest();").html("我要购买").removeClass("gbtn-disabled");
                  }
              }
          },
          error: function() {
        	  coderefresh();
        	  errorTip.html("投资失败，请刷新页面或稍后再试");
          }
      });

}
//计算可使用红包金额
function calAvailableCouponAmount(){
    var inputAmount = Number($("#inputAmount").val());
    var availableCouponAmount = $(".availableCouponAmount");
    for(var i=0,len=availableCouponAmount.length;i<len;i++){
        var leftAmount = Number($(availableCouponAmount[i]).attr("leftamount"));
        var usageType = Number($(availableCouponAmount[i]).attr("usagetype"));
        var discountRatio = Number($(availableCouponAmount[i]).attr("discountratio"));
        var discountAmount = Number($(availableCouponAmount[i]).attr("discountamount"));
        var couponId = $(availableCouponAmount[i]).attr("couponid");
        var typeTitle = $(availableCouponAmount[i]).attr("typetitle");
        var useCoupon=$('#coupon' + couponId);
        if(usageType == 800){
            var availableAmount = inputAmount*discountRatio;
            if(availableAmount > leftAmount){
              availableAmount = leftAmount;
            }
        }else{
            var availableAmount = leftAmount > discountAmount ? discountAmount : leftAmount;
        }
       
        if(isNaN(availableAmount)){
            availableAmount = 0;
        }
        useCoupon.html(typeTitle + "￥" + availableAmount.toFixed(2));
        useCoupon.attr("useCouponAmount", availableAmount.toFixed(2));
    }
}
</script>
</body>
</html>